﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Logging;
using Flower.Services;
using Flower.Services.Data;
using System.Collections.Concurrent;
using System.ServiceModel;

namespace Flower.Client
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    internal class CallbackQueue : IQueueService
    {
        private static readonly Log Log = LogManager.CreateLog(typeof(CallbackQueue).FullName);

        private readonly ConcurrentDictionary<string, Action<Blob>> _recipients =
            new ConcurrentDictionary<string,Action<Blob>>();

        public void AddRecipient(string recipient, Action<Blob> handler)
        {
            _recipients.AddOrUpdate(recipient, handler, (rcp, h) => handler);
        }

        public void RemoveRecipient(string recipient)
        {
            Action<Blob> handler;
            _recipients.TryRemove(recipient, out handler);
        }

        public Guid Enqueue(string recipient, Blob message)
        {
            try
            {
                Action<Blob> handler;

                if (_recipients.TryGetValue(recipient, out handler))
                {
                    handler(message);
                }

                return new Guid();
            }
            catch(Exception ex)
            {
                Log.Error("Enqueue failed.", ex);
                throw;
            }
        }

        public Blob Dequeue(string recipient, string waiterPid)
        {
            Log.Error("Dequeue is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }

        public IList<string> GetRecipients(long start, long limit)
        {
            Log.Error("GetRecipients is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }

        public IList<QueueServiceMessage> GetMessagesById(IList<Guid> ids)
        {
            Log.Error("GetMessagesById is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }

        public IList<QueueServiceMessage> GetMessagesByRecipient(IList<string> recipients, DateTime? start, DateTime? end)
        {
            Log.Error("GetMessagesByRecipient is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }

        public IList<Recipient> ExportRecipients(IList<string> recipients)
        {
            Log.Error("ExportRecipients is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }

        public void ImportRecipients(IList<Recipient> recipients)
        {
            Log.Error("ImportRecipients is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }

        public void RemoveMessages(IList<Guid> ids)
        {
            Log.Error("RemoveMessages is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }

        public void RemoveRecipients(IList<string> recipients)
        {
            Log.Error("RemoveRecipients is not supported by CallbackQueue.");
            throw new NotSupportedException();
        }
    }
}
